﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Twi.COMMON.Core
{
    /**
     * Author:feitianxinhong
     * Create Date:2015-04-29
     * Description:Dictionary扩展方法
     */
    public static class ExtensionDictionary
    {
        /// <summary>
        /// 根据关键字查找字典对应值，如果不存在或抛异常
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="key">关键字</param>
        /// <param name="notFoundErrorMsg">未找到关键字抛异常的信息，不传会使用设定的默认信息</param>
        /// <returns></returns>
        public static dynamic GetMustValue(this Dictionary<string, dynamic> dict, string key, string notFoundErrorMsg=null) 
        {
            if (dict != null && dict.ContainsKey(key))
            {
                return dict[key];
            }
            else {
                notFoundErrorMsg = notFoundErrorMsg ?? string.Format("给定的关键字'{0}'不在字典中或必填参数'{0}'未传值",key);
                throw new Exception(notFoundErrorMsg);
            }
        }
    }
}
